<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pgmpy.models.JunctionTree &#8212; pgmpy 0.1.2 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '0.1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../../index.html">
              <img class="logo" src="../../../_static/logo.png" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for pgmpy.models.JunctionTree</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python3</span>

<span class="kn">import</span> <span class="nn">networkx</span> <span class="k">as</span> <span class="nn">nx</span>

<span class="kn">from</span> <span class="nn">pgmpy.models</span> <span class="k">import</span> <span class="n">ClusterGraph</span>


<div class="viewcode-block" id="JunctionTree"><a class="viewcode-back" href="../../../models.html#pgmpy.models.JunctionTree.JunctionTree">[docs]</a><span class="k">class</span> <span class="nc">JunctionTree</span><span class="p">(</span><span class="n">ClusterGraph</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Class for representing Junction Tree.</span>

<span class="sd">    Junction tree is undirected graph where each node represents a clique</span>
<span class="sd">    (list, tuple or set of nodes) and edges represent sepset between two cliques.</span>
<span class="sd">    Each sepset in G separates the variables strictly on one side of edge to</span>
<span class="sd">    other.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    data: input graph</span>
<span class="sd">        Data to initialize graph. If data=None (default) an empty graph is</span>
<span class="sd">        created. The data is an edge list.</span>

<span class="sd">    Examples</span>
<span class="sd">    --------</span>
<span class="sd">    Create an empty JunctionTree with no nodes and no edges</span>

<span class="sd">    &gt;&gt;&gt; from pgmpy.models import JunctionTree</span>
<span class="sd">    &gt;&gt;&gt; G = JunctionTree()</span>

<span class="sd">    G can be grown by adding clique nodes.</span>

<span class="sd">    **Nodes:**</span>

<span class="sd">    Add a tuple (or list or set) of nodes as single clique node.</span>

<span class="sd">    &gt;&gt;&gt; G.add_node((&#39;a&#39;, &#39;b&#39;, &#39;c&#39;))</span>
<span class="sd">    &gt;&gt;&gt; G.add_nodes_from([(&#39;a&#39;, &#39;b&#39;), (&#39;a&#39;, &#39;b&#39;, &#39;c&#39;)])</span>

<span class="sd">    **Edges:**</span>

<span class="sd">    G can also be grown by adding edges.</span>

<span class="sd">    &gt;&gt;&gt; G.add_edge((&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;b&#39;))</span>

<span class="sd">    or a list of edges</span>

<span class="sd">    &gt;&gt;&gt; G.add_edges_from([((&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;b&#39;)),</span>
<span class="sd">    ...                   ((&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;c&#39;))])</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ebunch</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">JunctionTree</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">ebunch</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">add_edges_from</span><span class="p">(</span><span class="n">ebunch</span><span class="p">)</span>

<div class="viewcode-block" id="JunctionTree.add_edge"><a class="viewcode-back" href="../../../models.html#pgmpy.models.JunctionTree.JunctionTree.add_edge">[docs]</a>    <span class="k">def</span> <span class="nf">add_edge</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Add an edge between two clique nodes.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        u, v: nodes</span>
<span class="sd">            Nodes can be any list or set or tuple of nodes forming a clique.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import JunctionTree</span>
<span class="sd">        &gt;&gt;&gt; G = JunctionTree()</span>
<span class="sd">        &gt;&gt;&gt; G.add_nodes_from([(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;b&#39;), (&#39;a&#39;, &#39;c&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; G.add_edges_from([((&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;b&#39;)),</span>
<span class="sd">        ...                   ((&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;c&#39;))])</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">u</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">()</span> <span class="ow">and</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">()</span> <span class="ow">and</span> <span class="n">nx</span><span class="o">.</span><span class="n">has_path</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Addition of edge between </span><span class="si">{u}</span><span class="s1"> and </span><span class="si">{v}</span><span class="s1"> forms a cycle breaking the &#39;</span>
                             <span class="s1">&#39;properties of Junction Tree&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">u</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">u</span><span class="p">),</span> <span class="n">v</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">v</span><span class="p">)))</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">JunctionTree</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">add_edge</span><span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>

<div class="viewcode-block" id="JunctionTree.check_model"><a class="viewcode-back" href="../../../models.html#pgmpy.models.JunctionTree.JunctionTree.check_model">[docs]</a>    <span class="k">def</span> <span class="nf">check_model</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Check the model for various errors. This method checks for the following</span>
<span class="sd">        errors. In the same time also updates the cardinalities of all the random</span>
<span class="sd">        variables.</span>

<span class="sd">        * Checks if clique potentials are defined for all the cliques or not.</span>
<span class="sd">        * Check for running intersection property is not done explicitly over</span>
<span class="sd">          here as it done in the add_edges method.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        check: boolean</span>
<span class="sd">            True if all the checks are passed</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">nx</span><span class="o">.</span><span class="n">is_connected</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;The Junction Tree defined is not fully connected.&#39;</span><span class="p">)</span>

        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">JunctionTree</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">check_model</span><span class="p">()</span></div>

<div class="viewcode-block" id="JunctionTree.copy"><a class="viewcode-back" href="../../../models.html#pgmpy.models.JunctionTree.JunctionTree.copy">[docs]</a>    <span class="k">def</span> <span class="nf">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a copy of JunctionTree.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        JunctionTree : copy of JunctionTree</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import JunctionTree</span>
<span class="sd">        &gt;&gt;&gt; G = JunctionTree()</span>
<span class="sd">        &gt;&gt;&gt; G.add_edges_from([((&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;b&#39;)), ((&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;c&#39;))])</span>
<span class="sd">        &gt;&gt;&gt; phi1 = DiscreteFactor([&#39;a&#39;, &#39;b&#39;], [1, 2], np.random.rand(2))</span>
<span class="sd">        &gt;&gt;&gt; phi2 = DiscreteFactor([&#39;a&#39;, &#39;c&#39;], [1, 2], np.random.rand(2))</span>
<span class="sd">        &gt;&gt;&gt; G.add_factors(phi1,phi2)</span>
<span class="sd">        &gt;&gt;&gt; modelCopy = G.copy()</span>
<span class="sd">        &gt;&gt;&gt; modelCopy.edges()</span>
<span class="sd">        [((&#39;a&#39;, &#39;b&#39;), (&#39;a&#39;, &#39;b&#39;, &#39;c&#39;)), ((&#39;a&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;b&#39;, &#39;c&#39;))]</span>
<span class="sd">        &gt;&gt;&gt; G.factors</span>
<span class="sd">        [&lt;DiscreteFactor representing phi(a:1, b:2) at 0xb720ee4c&gt;,</span>
<span class="sd">         &lt;DiscreteFactor representing phi(a:1, c:2) at 0xb4e1e06c&gt;]</span>
<span class="sd">        &gt;&gt;&gt; modelCopy.factors</span>
<span class="sd">        [&lt;DiscreteFactor representing phi(a:1, b:2) at 0xb4bd11ec&gt;,</span>
<span class="sd">         &lt;DiscreteFactor representing phi(a:1, c:2) at 0xb4bd138c&gt;]</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">copy</span> <span class="o">=</span> <span class="n">JunctionTree</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">edges</span><span class="p">())</span>
        <span class="n">copy</span><span class="o">.</span><span class="n">add_nodes_from</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">())</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">:</span>
            <span class="n">factors_copy</span> <span class="o">=</span> <span class="p">[</span><span class="n">factor</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">]</span>
            <span class="n">copy</span><span class="o">.</span><span class="n">add_factors</span><span class="p">(</span><span class="o">*</span><span class="n">factors_copy</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">copy</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2016, Ankur Ankan.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.1.
    </div>
  </body>
</html>